import os
import random
import sys
import time

from PyQt5.QtCore import QPropertyAnimation, QRect, QUrl, QTimer, QStringListModel, QTime, Qt
from PyQt5.QtGui import QColor, QPainter, QPalette, QBrush, QPixmap, QStandardItemModel, QStandardItem, QIcon
from PyQt5.QtMultimedia import QMediaContent
from PyQt5.QtWidgets import QWidget, QApplication, QLineEdit, QMessageBox, QPushButton, QLabel, QLCDNumber, QFileDialog, \
    QGridLayout
from PyQt5 import QtWidgets, QtGui, QtCore

from untitled import Ui_Dialog_friut
from yc import yycc



class mainWindow(QWidget, Ui_Dialog_friut):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.ui_init()
        self.setWindowTitle('主界面')

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.show_next_image)

        self.slot_init()
        self.model = QStandardItemModel()



    def ui_init(self):
        pass

    def slot_init(self):
        pass
        self.pushButton_train.clicked.connect(self.train_click)
        self.pushButton_friut.clicked.connect(self.friut_click)
        self.pushButton_begin.clicked.connect(self.begin_click)


    def train_click(self):
        print('train_model1')

    def friut_click(self):
        print('friut')
        # 打开文件选择对话框
        file_dialog = QFileDialog()
        file_path, _ = file_dialog.getOpenFileName(self, '选择图片文件', '', '图片文件 (*.png *.jpg *.jpeg)')
        # print(file_path)
        if file_path:
            # 加载图片
            pixmap = QPixmap(file_path)
            # 创建一个 QLabel 用于显示图片
            label = QtWidgets.QLabel(self.gridLayoutWidget)
            label.setPixmap(pixmap)
            label.setScaledContents(True)  # 让图片适应标签大小
            # 获取 gridLayoutWidget 的布局，如果没有则创建一个
            if self.gridLayoutWidget.layout() is None:
                grid_layout = QtWidgets.QGridLayout(self.gridLayoutWidget)
            else:
                grid_layout = self.gridLayoutWidget.layout()
            # 将标签添加到网格布局中
            grid_layout.addWidget(label, 0, 0)
        cla = yycc(file_path)
        self.textEdit_group.clear()
        self.textEdit_group.append(str(cla))
        print(cla)



    def begin_click(self):
        print('begin')
        folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹')
        self.file_paths = []
        if folder_path:
            # 遍历文件夹获取所有文件路径
            for root, dirs, files in os.walk(folder_path):
                for file in files:
                    self.file_paths.append(os.path.join(root, file))
            print(self.file_paths)
            self.current_index = 0

            self.show_next_image()
            self.timer.start(1000)  # 每隔 1 秒切换一次图片

    def show_next_image(self):
        # 检查 self.gridLayoutWidget 是否已经设置了布局
        if self.gridLayoutWidget.layout():
            # 反向遍历布局中的所有子项。使用 reversed 是因为在删除子项时，正向遍历会导致索引错乱
            for i in reversed(range(self.gridLayoutWidget.layout().count())):
                # 获取布局中指定索引位置的子项对应的 widget
                # self.gridLayoutWidget.layout().itemAt(i) 会返回一个 QLayoutItem 对象，
                # 调用其 widget() 方法可以获取对应的 QWidget 对象
                widget = self.gridLayoutWidget.layout().itemAt(i).widget()
                # 检查获取到的 widget 是否存在（不为 None）
                if widget:
                    # 调用 deleteLater() 方法来计划删除该 widget。
                    # 该方法不会立即删除 widget，而是将删除操作放入事件队列中，
                    # 等到下一次事件循环时再执行，这样可以避免在删除过程中出现的一些问题
                    widget.deleteLater()

            path = self.file_paths[self.current_index]
            pixmap = QPixmap(path)
            if not pixmap.isNull():
                label = QLabel(self.gridLayoutWidget)
                label.setPixmap(pixmap)
                label.setScaledContents(True)

                if self.gridLayoutWidget.layout() is None:
                    grid_layout = QGridLayout(self.gridLayoutWidget)
                else:
                    grid_layout = self.gridLayoutWidget.layout()

                grid_layout.addWidget(label, 0, 0)

            cla = yycc(path)
            self.textEdit_group.clear()
            self.textEdit_group.append(str(cla))
            print(cla)

            self.current_index = (self.current_index + 1) % len(self.file_paths)





if __name__ == '__main__':
    app = QApplication(sys.argv)
    login_window = mainWindow()
    login_window.show()
    sys.exit(app.exec_())